package com.client.service;

import com.client.Initialize;
import com.client.entity.B_client;
import com.client.sm9.Main;
import com.client.sm9.gm.sm9.*;
import com.client.utils.MessageUtils;
import com.client.utils.UDPUtil;
import com.client.utils.Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
//密钥交换
public class ExchangeService {

    //计算R
    //判断对方的R是否属于G
    @Autowired
    KeyService keyService;
    @Autowired
    MigrationService migrationService;
    @Autowired
    UDPUtil udp;

    SM9Curve sm9Curve = new SM9Curve();
    SM9 sm9 = new SM9(sm9Curve);

    //密钥长度 单位：byte
    int keyByteLen = 16;

    public G1KeyPair Exchange1(B_client client) {
        if (!Initialize.HPS) {
            G1KeyPair g1KeyPair = sm9.keyExchangeInit(keyService.getPpub_e(), client.getGoal_id());
            return g1KeyPair;
        } else {
            migrationService.Exchange1(client);
            return null;
        }
    }

    public ResultKeyExchange Exchange2(String goal_id, boolean isSponsor, byte[] R, G1KeyPair myTempKeyPair) {
        G1PublicKey goalPublicKey = G1PublicKey.fromByteArray(sm9Curve, R);
        ResultKeyExchange AgreementKey = null;
        try {
            AgreementKey = sm9.keyExchange(keyService.getPpub_e(), isSponsor,
                    keyService.getId(), goal_id, keyService.getD_exchange(), myTempKeyPair, goalPublicKey, keyByteLen);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return AgreementKey;
    }

    //密钥交换请求1
    public byte[] exchange1(B_client client) {
        Map<String, Object> body = new HashMap<>();

        body.put("R", client.getMyKeyPair().getPublicKey().toByteArray());
        body.put("id", keyService.getId());
        body.put("type", "EXCHANGE_ONE");
        body.put("uid", client.getUid());
        body.put("uuid", MessageUtils.getuuid());
        body.put("time", MessageUtils.getNowTime());
        body.put("hash", MessageUtils.HashOfExchange(body,client.getGoal_n()));

        return Utils.getByteFromObjectMap(body);
    }
    //密钥交换请求2
    public byte[] exchange2(B_client client) {
        Map<String, Object> body = new HashMap<>();

        body.put("id", keyService.getId());
        body.put("type", "EXCHANGE_TWO");
        body.put("uid", client.getUid());
        body.put("uuid", MessageUtils.getuuid());
        body.put("time", MessageUtils.getNowTime());
        body.put("R", client.getMyKeyPair().getPublicKey().toByteArray());
        body.put("S", client.getResultKeyExchange().getSB1());
        body.put("hash", MessageUtils.HashOfExchange(body,client.getGoal_n()));

        return Utils.getByteFromObjectMap(body);
    }
    //密钥交换请求3
    public byte[] exchange3(B_client client) {
        Map<String, Object> body = new HashMap<>();

        body.put("id", keyService.getId());
        body.put("type", "EXCHANGE_THREE");
        body.put("uid", client.getUid());
        body.put("uuid", MessageUtils.getuuid());
        body.put("time", MessageUtils.getNowTime());
        body.put("S", client.getResultKeyExchange().getSA2());
        body.put("hash", MessageUtils.HashOfExchange(body,client.getGoal_n()));

        return Utils.getByteFromObjectMap(body);
    }
    //密钥交换请求4
    public byte[] exchange4(B_client client) {
        Map<String, Object> body = new HashMap<>();

        body.put("id", keyService.getId());
        body.put("type", "EXCHANGE_FOUR");
        body.put("uid", client.getUid());
        body.put("uuid", MessageUtils.getuuid());
        body.put("time", MessageUtils.getNowTime());
        body.put("hash", MessageUtils.HashOfExchange(body,client.getGoal_n()));

        return Utils.getByteFromObjectMap(body);
    }
}
